package com.bac.rentmap.ad.flat.crawler.hataby;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.logging.Logger;
import java.util.regex.Pattern;

import org.htmlcleaner.ContentToken;
import org.htmlcleaner.TagNode;
import org.htmlcleaner.XPatherException;
import org.springframework.beans.factory.annotation.Required;

import com.bac.rentmap.ad.crawler.Context;
import com.bac.rentmap.ad.crawler.phone.PhoneNormalizer;
import com.bac.rentmap.ad.crawler.stepped.AbstractSteppedAdCrawler;
import com.bac.rentmap.ad.flat.crawler.FlatCrawledAd;
import com.bac.rentmap.ad.flat.crawler.ParsingHelper;

public class HataBySteppedAdCrawler extends
        AbstractSteppedAdCrawler<FlatCrawledAd> {
    private static final Logger log = Logger
            .getLogger(HataBySteppedAdCrawler.class.getName());

    private Pattern pricePattern = Pattern.compile("\\$(\\d+)");

    private ParsingHelper parsingHelper;
    private PhoneNormalizer phoneNormalizer;

    private int page;

    public HataBySteppedAdCrawler(Context context) {
        super();
        if (context.getPage() != null) {
            page = context.getPage() + 1;
        } else {
            page = 1;
        }
    }

    @Override
    protected String getItemXPath() {
        return "//body/div/table/tbody/tr/td/div/table/tbody/tr/td/div/div/div";
    }

    @Override
    protected String getNextUrl() {
        String res = "http://hata.by/board/page/" + page++ + "/4/?photo=false";
        log.info("Requesting URL " + res);
        return res;
    }

    @Override
    protected FlatCrawledAd parseCrawledAd(TagNode tagNode)
            throws XPatherException {
        FlatCrawledAd res = new FlatCrawledAd();
        // String title = getValue(tagNode, "/tr[1]/th[1]/text()");
        String header = getValue(tagNode, "/div/table/tbody/tr[1]/th[1]/text()");
        String description = getDescription(tagNode);
        res.setDescription(description);
        parsingHelper.mayBeParseStreetAndBuilding(header, description, res);

        String priceString = getValue(tagNode,
                "/div/table/tbody/tr[2]/td/span[@class='bprice']/text()");
        res.setPrice(parsingHelper.parsePrice(priceString, pricePattern));

        String dateString = getValue(tagNode,
                "/div/table/tbody/tr[1]/th[2]/text()");
        SimpleDateFormat format = new SimpleDateFormat("dd.MM.yy");
        try {
            log.info("Parsing publishing date from " + dateString);
            res.setPublishingDate(format.parse(dateString));
        } catch (ParseException e) {
            log.warning("Can't parse publishing date from " + dateString);
        }
        String phoneString;
        try {
            phoneString = getValue(tagNode,
                    "/div/table/tbody/tr[2]/td/b[3]/text()");
        } catch (IllegalArgumentException e) {
            // not agent
            phoneString = getValue(tagNode,
                    "/div/table/tbody/tr[2]/td/b[2]/text()");
        }
        res.setPhone(phoneNormalizer.normalizePhone(phoneString));
        res.setRoom(parsingHelper.parseRoom(header));
        res.setCity(parseCity(tagNode));
        res.setSourceUrl("http://hata.by");
        res.setReadyForTransformation(true);
        return res;
    }

    private String parseCity(TagNode rootNode) throws XPatherException {
        String cityString = getValue(rootNode, "/div[1]/span/text()");
        if ("Минск".equals(cityString)) {
            return "minsk";
        }
        throw new IllegalArgumentException("Can't parse city from "
                + cityString);
    }

    private String getDescription(TagNode tagNode) throws XPatherException {
        Object[] xpathValue = tagNode
                .evaluateXPath("/div/table/tbody/tr[2]/td");
        TagNode tdNode = (TagNode) xpathValue[0];
        log.warning("Getting description from " + tagNode.getText());
        for (Object child : tdNode.getChildren()) {
            if (child instanceof ContentToken) {
                String res = ((ContentToken) child).getContent();
                return convertHtml2text(res);
            }
        }
        throw new IllegalArgumentException("Can't parse description from "
                + tagNode.getText());
    }

    @Required
    public void setParsingHelper(ParsingHelper parsingHelper) {
        this.parsingHelper = parsingHelper;
    }

    @Required
    public void setPhoneNormalizer(PhoneNormalizer phoneNormalizer) {
        this.phoneNormalizer = phoneNormalizer;
    }
}
